
//动态渲染css
function loadCSS(cssUrls) {
    cssUrls.forEach(url => {
        var link = document.createElement('link');
        link.rel = 'stylesheet';
        link.type = 'text/css';
        link.href = url;
        document.head.appendChild(link);
    });
}

//动态渲染script
function loadScript(src, callback) {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = src;
    script.onload = callback;
    document.body.appendChild(script);
}

//动态获取绝对路径
function loadPath() {
    var currentURL = window.location.href;
    var relativePath = currentURL.substring(
        currentURL.lastIndexOf("view"),
        currentURL.length
    );
    var str = "";
    for (let i = 0; i < countOccurrences(relativePath, "/"); i++) {
        str = str.concat("../");
    }
    function countOccurrences(str, char) {
        const regex = new RegExp(char, "g");
        const matches = str.match(regex);
        return matches ? matches.length : 0;
    }
    return str;
}

loadCSS([loadPath() + 'yl/index.css',loadPath() + 'style/pdf_viewer.min.css']);

//渲染组件
function PluginScript() {
    return Promise.all([
        new Promise(resolve => {
            loadScript(loadPath() + "utils/utils.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "utils/ID_Validity.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "utils/compressor.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "utils/mixins.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "yl/vue.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "utils/jquery-3.6.0.min.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "utils/jquery.cookie.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "config/config.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "yl/index.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "yl/zh-cn.mjs", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "yl/index.iife.min.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "api/request.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "utils/xlsx.full.min.js", resolve);
        }),
        new Promise(resolve => {
            loadScript(loadPath() + "utils/pdf.min.js", resolve);
        }),
    ])
}